#ifndef _WORK_QUEUE_H_
#define _WORK_QUEUE_H_

#include "def.h"
#include "mutex.h"
#include "task.h"

#ifdef __cplusplus
extern "C"{
#endif

typedef void (*work_func)(void *arg);
typedef struct work work_t;
typedef struct worker worker_t;

/******************************************************************************
 *                         work
 *****************************************************************************/
struct work {
    work_t      *next;
    void        *arg; 
    work_func   func;
};

static inline void work_init(work_t *self, void *arg, work_func func)
{
	if (arg)
		self->arg = arg;
	else
		self->arg = self;
    self->func = func;
    self->next = NULL;
}

static inline void work_term(work_t *self)
{
    memset(self, 0, sizeof(work_t));
}

static inline work_t *work_new(void *arg, work_func func)
{
    work_t *self = (work_t *)MALLOC(sizeof(work_t));
    if (self) {
        work_init(self, arg, func);
    }

    return self;
}

static inline void work_destroy(work_t **p_self)
{
    if (p_self && *p_self) {
        work_t *self = *p_self;
        work_term(self);
        safe_free(*p_self);
    }
}

static inline int work_pending(work_t *self)
{
	return (self->next) ? 1 : 0;
}


// int worker_init(worker_t *self);
// void worker_term(worker_t *self);
worker_t *worker_new(void);
void worker_destroy(worker_t **p_self);
void worker_add(worker_t *self, work_t *w);
void worker_add_tail(worker_t *self, work_t *w);
uint32_t worker_nums(worker_t *self);

#ifdef __cplusplus
}
#endif

#endif
